package com.demo.morning;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName StreamDemo
 * @Description TODO
 * @Author tianqinglong
 * @Date 9:52 2022/7/12
 * @Version 1.0
 **/
public class StreamDemo {
    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        //        System.out.println(authors);
                //打印所有年龄小于18的作家的名字，并且要注意去重
              /*  authors.stream()//把集合转换成流
                        .distinct() //去重
                        .filter(author -> author.getAge()<18) //筛选年龄小于18的
                        .forEach(author -> System.out.println(author.getName())); //打印
        */
        /*
                authors.stream()
                        .map(author -> author.getAge())
                        .map(age->age+10)
                        .skip(1)
                        .forEach(s -> System.out.println(s));
        */
         //打印除了年龄最大的作家外的其他作家 要求不能有重复元素，并且年龄降序
             /*   authors.stream()
                        .distinct()
                        .sorted((o1, o2) -> o2.getAge()-o1.getAge()) //降序
                        .skip(1)
                        .forEach(author -> System.out.println(author.getName()));
        */
            /*    Optional<Integer> min = authors.stream()
                        .flatMap(author -> author.getBooks().stream())
                        .map(book -> book.getScore())
                        .min((score1, score2) -> score1 - score2);
                System.out.println(min.get());
        */
        Author author = new Author(1L, "梦多", 33, "梦开始的地方", null);
        Optional<Author> author1 = Optional.ofNullable(author);
        author1.ifPresent(author2-> System.out.println(author2.getBooks()));
    }

    private static List<Author> getAuthors(){
        Author author = new Author(1L, "zhangsan1", 33, "张三", null);
        Author author2 = new Author(2L, "zhangsan2", 15, "张三", null);
        Author author3 = new Author(3L, "zhangsan3", 14, "张三", null);
        Author author4 = new Author(3L, "zhangsan4", 14, "张三", null);
        List<Book> book1 = new ArrayList<Book>();
        List<Book> book2 = new ArrayList<Book>();
        List<Book> book3 = new ArrayList<Book>();
        book1.add(new Book(1L,"光明1","哲学",88,"光明与黑暗的战斗"));
        book1.add(new Book(2L,"光明2","哲学",77,"光明与黑暗的战斗"));
        book2.add(new Book(3L,"光明3","哲学",66,"光明与黑暗的战斗"));
        book2.add(new Book(3L,"光明3","哲学",55,"光明与黑暗的战斗"));
        book2.add(new Book(4L,"光明4","哲学",44,"光明与黑暗的战斗"));
        book3.add(new Book(5L,"光明5","哲学",33,"光明与黑暗的战斗"));
        book3.add(new Book(6L,"光明6","哲学",22,"光明与黑暗的战斗"));
        book3.add(new Book(6L,"光明6","哲学",11,"光明与黑暗的战斗"));
        author.setBooks(book1);
        author2.setBooks(book2);
        author3.setBooks(book3);
        author4.setBooks(book3);
        ArrayList<Author> authorList = new ArrayList<Author>(Arrays.asList(author, author2, author3, author4));
        return authorList;

    }
}
